package com.gisterrier.client.components;

import com.common.shared.map.GisMapClickedObject;
import com.gisterrier.client.GisTerrier;
import com.gisterrier.shared.BuildingCustomer;
import com.gisterrier.shared.GissTigerConstants;
import com.gisterrier.shared.ObjSelectionresult;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class PSearchContainer extends HLayout {

	private SectionStack sectionStackMap;
	private SectionStack sectionStackBuiling;
	private PBuildingCustomer pBuildingCustomer;
	private PMapBuildingSearch mapBuildingSearch;

	public PSearchContainer() {
		super();
		this.setWidth("20%");

		sectionStackMap = new SectionStack();
		sectionStackMap.setWidth100();

		mapBuildingSearch = new PMapBuildingSearch();

		SectionStackSection mapObjectSearch = new SectionStackSection();
		mapObjectSearch.setTitle("Map Building Search");
		mapObjectSearch.setID("Map_Building_Search");
		mapObjectSearch.setExpanded(true);
		mapObjectSearch.setItems(mapBuildingSearch);
		sectionStackMap.addSection(mapObjectSearch);

		PMapStreetTool pMapStreetTool = new PMapStreetTool();

		SectionStackSection mapStreetTool = new SectionStackSection();
		mapStreetTool.setTitle("Map Street Tool");
		mapStreetTool.setExpanded(true);
		mapStreetTool.setItems(pMapStreetTool);
		sectionStackMap.addSection(mapStreetTool);

		PMapGasNetwork pMapGasNetwork = new PMapGasNetwork();

		SectionStackSection mapGasNetwork = new SectionStackSection();
		mapGasNetwork.setTitle("Gas Network");
		mapGasNetwork.setExpanded(true);
		mapGasNetwork.setItems(pMapGasNetwork);
		sectionStackMap.addSection(mapGasNetwork);

		VLayout cont = new VLayout();
		sectionStackMap.setWidth100();
		sectionStackMap.setHeight100();
		sectionStackMap.setHeight("50%");
		sectionStackMap.setShowResizeBar(true);

		cont.addMember(sectionStackMap);

		sectionStackBuiling = new SectionStack();
		sectionStackBuiling.setWidth100();

		PBillingCustSearch pBillingCustSearch = new PBillingCustSearch();

		SectionStackSection customerSearch = new SectionStackSection();
		customerSearch.setTitle("Billing Customer Search");
		customerSearch.setExpanded(true);
		customerSearch.setItems(pBillingCustSearch);
		sectionStackBuiling.addSection(customerSearch);

		PBillingAddressSearch pAddressSearch = new PBillingAddressSearch();

		SectionStackSection addressSearch = new SectionStackSection();
		addressSearch.setTitle("Billing Address Search");
		addressSearch.setExpanded(true);
		addressSearch.setItems(pAddressSearch);
		sectionStackBuiling.addSection(addressSearch);

		pBuildingCustomer = new PBuildingCustomer();

		SectionStackSection mapBuildCust = new SectionStackSection();
		mapBuildCust.setID("_BuildingCustomer");
		mapBuildCust.setTitle("Billing customer by street");
		mapBuildCust.setExpanded(true);
		mapBuildCust.setItems(pBuildingCustomer);
		sectionStackBuiling.addSection(mapBuildCust);

		sectionStackBuiling.setHeight("50%");
		cont.addMember(sectionStackBuiling);

		this.addMember(cont);

	}

	private void setMapBuildingResult(final GisMapClickedObject clObj,
			BuildingCustomer result) {
		sectionStackMap.expandSection("Map_Building_Search");
		sectionStackBuiling.expandSection("_BuildingCustomer");
		mapBuildingSearch.setResult(result, clObj);
		pBuildingCustomer.setResult(result, clObj);
		
	}

	public void buildingCustomersChanged() {
		mapBuildingSearch.refresh();
	}

	public void setSelectedMapObject(final GisMapClickedObject clObj) {
		if (clObj.getObj_type() == GissTigerConstants.MOT_BUILDING) {

			GisTerrier.gissTerrierService.getBuildingCustomer(clObj,
					new AsyncCallback<BuildingCustomer>() {

						@Override
						public void onSuccess(BuildingCustomer result) {
							setMapBuildingResult(clObj, result);
						}

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}
					});
		}
	}
}
